// pages/home/home.js
Page({
  data: {
    //用户名称密码
    username: '',
    password: '',
    userMes: {},

    leftname: [],
    foodname:[],
    totalCost:150,
    orderNumber:0,
    activeIndex:4,
    scrollTop:100,
    toView:'a0',
    cartList:[],
    currentType:0,
    currentIndex:0,
    sumMoney:0,
    showCart:false,
    toastHidden:true,

    swiperImg: [
      { src: "https://www.heytea.com/img/bg1.jpg" },
      { src: "https://www.heytea.com/img/banner1.jpg" },
      { src: "https://www.heytea.com/img/banner2.jpg" },
      { src: "https://www.heytea.com/img/banner3.jpg" }
    ],

    listData:[] 
  },

  //提交订单
  submitClick:function(){
    if(this.data.sumMoney!=0){
      wx.setStorageSync('cartList', this.data.cartList);  //购物车列表
      wx.setStorageSync('sumMoney', this.data.sumMoney);  //价格
      wx.setStorageSync('orderNumber', this.data.orderNumber);   //数量
      wx.navigateTo({
        url: '../list/list'
      }) 
    }
    if(this.data.sumMoney == 0){
    }
  },

//选择左侧列表菜单函数
  selectMenu: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      activeIndex: index,
      toView: 'a' + index,
    })
  },

  scroll:function(e){
    var dis=e.detail.scrollTop
    if(dis>0 && dis<300){
      this.setData({ activeIndex: 0 })
    }
    if(dis>300 && dis<600){
      this.setData({ activeIndex: 1 })
    }
    if(dis>600 && dis<900){
      this.setData({ activeIndex: 2 })
    }
    if (dis > 900 && dis < 1050) {
      this.setData({ activeIndex: 3 })
    }
    if (dis > 1200 && dis < 1350) {
      this.setData({ activeIndex: 4 })
    }
    if (dis > 1350 && dis < 1800) {
      this.setData({ activeIndex: 5 })
    }

  },

  addToCart: function (e) {
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    this.setData({
      currentType: type,
      currentIndex: index,
    });
    var a = this.data
    var addItem = {
      "name": a.listData[a.currentType].foods[a.currentIndex].name,
      "price": a.listData[a.currentType].foods[a.currentIndex].price,
      "number": 1,
      "sum": a.listData[a.currentType].foods[a.currentIndex].price,
      "id": a.listData[a.currentType].foods[a.currentIndex].id,
    }
    var sumMoney = a.sumMoney + a.listData[a.currentType].foods[a.currentIndex].price;
    var cartList = this.data.cartList;
    cartList.push(addItem);
    this.setData({
      cartList: cartList,
      showModalStatus: false,
      sumMoney: sumMoney,
      orderNumber: a.orderNumber + 1
    });
    console.log(this.data.cartList)
    this.setData({
      toastHidden:false
    });
    
    var _this = this;
    setTimeout(function () {
      _this.setData({
        toastHidden: true
      });
    }, 1000);
  },

  showCart:function(){
    console.log(this.data.showCart)
    if (this.data.cartList.length != 0) {
      this.setData({
        showCart: !this.data.showCart,
      });
    }
  },

  clearCart:function(){
    this.setData({
      cartList:[],
      showCart:false,
      sumMoney:0,
      orderNumber:0,
    })
  },

  addNumber: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var cartList = this.data.cartList;
    cartList[index].number++;
    var sum = this.data.sumMoney + cartList[index].price;
    cartList[index].sum += cartList[index].price;

    this.setData({
      cartList: cartList,
      sumMoney: sum,
      orderNumber: this.data.orderNumber + 1
    });
  },

  decNumber: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var cartList = this.data.cartList;

    var sum = this.data.sumMoney - cartList[index].price;
    cartList[index].sum -= cartList[index].price;
    cartList[index].number == 1 ? cartList.splice(index, 1) : cartList[index].number--;
    this.setData({
      cartList: cartList,
      sumMoney: sum,
      showCart: cartList.length == 0 ? false : true,
      orderNumber: this.data.orderNumber - 1
    });
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    this.setData({
      userMes: wx.getStorageSync('userInfo'),
    })

    //获取菜单
    // console.info("testmessag is: "+that.data.token);
    wx.request({
      url: "http://localhost:8080/wechat/goods",
      method: "GET",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Content-Type': 'application/json'
        // 'Authorization': that.data.token
      },

      success: function (res) {
        var arr=new Array();
        console.info(res.statusCode+1);
        console.info(res.data);
        console.info(that.data.userMes);
        if (res.statusCode == 200) {
          for (var index in res.data){
           let type=res.data[index].type;
            arr[index]= type;   //arr = type类型
          }
          that.setData({
            leftname:arr
          })
          console.info(res);
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期1，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })

    wx.request({
      url: "http://localhost:8080/wechat/goods",
      method: "GET",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        // 'Authorization': that.data.token
      },
      success: function (res) {
        var arr=new Array();
        console.info(res.statusCode+2);
        if (res.statusCode == 200) {
          for (var index in that.data.leftname){
            var obj ={
              name:that.data.leftname[index],
              foods:[]
            }
            arr.push(obj);
            console.info(obj);
          }
            for(var index in res.data){
            var obj={
              name:res.data[index].name,    // 食品传参（名字、图片链接、价格、食品id
              image_url:res.data[index].pic,
              price:res.data[index].price_out,
              id:res.data[index].goods_id,
            }
            arr[res.data[index].goods_id-1].foods.push(obj)
            }
    
          console.log(arr)
          that.setData({
              listData:arr
          })
           console.log(that.listData)
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期2，请重新登录");
          wx.redirectTo({
            url: '/pages/openid/openid',
          })
        }
      },
      fail: () => {
        // fail
      },
    })
    //从数据库中获取数据
    var sysinfo = wx.getSystemInfoSync().windowHeight;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})